import { Injectable } from '@angular/core'
import { HttpClient } from '@angular/common/http'
import { Observable, throwError } from 'rxjs'
import { catchError } from 'rxjs/operators'

import { COMB_URL } from 'app/utils/contraints'
import { IMCC, IMCCQuery } from './mcc-select.interface'
import { IGenericPagination } from '@shared/interfaces/pagination'

@Injectable({
  providedIn: 'root',
})
export class MccService {
  constructor(private http: HttpClient) {}

  getList(query: IMCCQuery): Observable<IGenericPagination<IMCC>> {
    const q = Object.keys(query)
      .filter(key => !!query[key])
      .map(key => `${key}=${query[key]}`)
      .join('&')
    let url = `${COMB_URL}/camel/public/mcc`
    if (q) {
      url += `?${q}`
    }

    return this.http.get<IGenericPagination<IMCC>>(url).pipe(
      catchError(err => {
        return throwError(err)
      }),
    )
  }
}
